package org.acb.sgp.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

public class FacesUtil implements Serializable {

	private static final long serialVersionUID = 1L;
	public static final int ERROR = 1;
    public static final int INFO = 2;
    public static final int WARN = 3;

    public static Object getSessionAttribute(String name) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) externalContext.getSession(false);
        Object o = null;
        if (session != null) {
            o = session.getAttribute(name);
        }
        return o;
    }

    public static void setSessionAttribute(String name, Object value) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) externalContext.getSession(false);
        session.setAttribute(name, value);
    }

    public static void removeSessionAttribute(String name) {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) externalContext.getSession(false);
        session.removeAttribute(name);
    }

//    public static User getUser() {
//        return (User) getSessionAttribute("user");
//    }
//
//    public static void setUser(User user) {
//        setSessionAttribute("user", user);
//    }
//
//    public static void removeUser() {
//        removeSessionAttribute("usuario");
//    }
//
//    public static boolean existUser() {
//        if (getUser() != null) {
//            return true;
//        } else {
//            return false;
//        }
//    }
    public static void showMessage(String message, int icon) {
        switch (icon) {
            case ERROR:
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
                break;
            case INFO:
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, message));
                break;
            case WARN:
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, message, message));
                break;
        }
    }

    public static void openDialog(String nameDialog) {
        RequestContext.getCurrentInstance().execute(nameDialog + ".show();");
    }

    public static void closeDialog(String nameDialog) {
        RequestContext.getCurrentInstance().execute(nameDialog + ".hide();");
    }

    public static void update(String url) {
        RequestContext.getCurrentInstance().update(url);
    }
    
    public static void createFileOnProject(byte[] bytes, String urlFile) {
        if (bytes != null) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
            File dir = new File(scontext.getRealPath("") + "/temp");
            if (!dir.exists()) {
                dir.mkdir();
            }
            urlFile = scontext.getRealPath(urlFile);
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(urlFile);
                fos.write(bytes);
                fos.close();
            } catch (IOException e) {
                System.out.println("Error al crear Archivo: " + e.getMessage());
            }
        }
    }

    public static void createFile(byte[] bytes, String urlFile) {
        if (bytes != null) {
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(urlFile);
                fos.write(bytes);
                fos.close();
            } catch (IOException e) {
                System.out.println("Error al crear Archivo: " + e.getMessage());
            }
        }
    }

    public static byte[] loadFile(String urlFile) {
        byte[] bytes = null;
        try {
            Path path = Paths.get(urlFile.toString());
            bytes = Files.readAllBytes(path);
        } catch (IOException ex) {
            Logger.getLogger(FacesUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bytes;
    }

    public static void removeFile(String urlFile) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ServletContext scontext = (ServletContext) facesContext.getExternalContext().getContext();
        File file = new File(scontext.getRealPath(urlFile));
        if (file.exists()) {
            file.delete();
        }
    }
}
